package com.frank.java8;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

/**
 * @author liujiang
 * @version v1.0
 * @description 本类主要是用于归纳总结java8 Stream的使用方式，后续会继续补充
 * @since 2019/8/5 22:40
 */
public class Lambda {

    public static void main(String[] args) {

        readMeFirst();

        final List<Cat> catList = initList();
        // 1. filter(Predicate<? super T> predicate) 过滤年龄小于等于3的猫咪
        List<Cat> ageLessThanThreeList = catList.parallelStream().filter(cat -> cat.getAge().compareTo(3) <= 0).collect(toCollection(LinkedList::new));
        System.out.println("------ filter line ------");
        ageLessThanThreeList.parallelStream().forEach(System.err::println);

        // 2. map(Function<? super T, ? extends R> mapper) 将英文名称全部大写
        List<String> upperCaseEnNameList = catList.parallelStream().map(cat -> cat.getEnName().toUpperCase()).collect(Collectors.toList());
        System.out.println("------ map line -------");
        upperCaseEnNameList.parallelStream().forEach(enName -> {
            System.err.print(enName + " ");
        });
        System.out.println();

        // 3. mapToInt(ToIntFunction<? super T> mapper) 对年龄求和
        int sumAge = catList.parallelStream().mapToInt(Cat::getAge).sum();
        System.out.println("------ mapToInt line -----");
        System.err.println(sumAge);

        // 4. flatMap(Function<? super T, ? extends Stream<? extends R>> mapper) 层级结构扁平化
        List<List<Integer>> flatMapList = flatMapInitList();
        List<Integer> numberList = flatMapList.parallelStream().flatMap(itemList -> itemList.parallelStream()).collect(toList());
        System.out.println("------ flatMap line ------");
        numberList.parallelStream().forEach(item -> {
            System.err.print(item.toString() + " ");
        });

        // 5. distinct 去重
        List<Cat> dupCatList = dupCatInitList();
        List<Cat> duplicateCatList = dupCatList.parallelStream().distinct().collect(toList());
        System.out.println();
        System.out.println("----- distinct for cat line (error) -------");
        duplicateCatList.stream().forEach(cat -> {
            // 因为没有重写hashCode 及 equals方法，所以得到的是两个Cat对象
            System.err.println(cat.toString());
        });
        // case two, 实现过滤去重
        List<Integer> dupIntegerLit = dupIntegerInitList();
        List<Integer> duplicateIntegerList = dupIntegerLit.parallelStream().distinct().collect(toList());
        System.out.println("------ distinct for Integer line (right) -----");
        duplicateIntegerList.parallelStream().forEach(System.out::print);
        System.out.println();

        // 6. sorted --> no args
        List<Integer> orderAgeList = catList.parallelStream().map(Cat::getAge).sorted().collect(toList());
        System.out.println("------ sorted line (no args) ------");
        orderAgeList.stream().forEach(System.out::print);
        System.out.println();
        System.out.println("attention：并行流会影响排序，需要特别注意!并行流结果为：");
        orderAgeList.parallelStream().forEach(System.out::print);

        // sorted(Comparator<? super T> comparator) --> has args [reverse order]
        List<Cat> orderedAgeOfCatList = catList.parallelStream().sorted(Comparator.comparing(Cat::getAge).reversed()).collect(toList());
        /*
            等效于：
            List<Cat> orderedAgeOfCatList = catList.parallelStream().sorted(Collections.reverseOrder(Comparator.comparing(Cat::getAge))).collect(toList());
            即：Comparator.comparing(Cat::getAge).reversed() = Collections.reverseOrder(Comparator.comparing(Cat::getAge))
         */
        System.out.println();
        System.out.println("-------- sorted line (has args)------");
        orderedAgeOfCatList.stream().forEach(cat -> {
            System.err.println(cat.toString());
        });

        // 使用java8 list.sort(Comparator<? super E> c)
        catList.sort(Comparator.comparing(Cat::getAge).thenComparing(Cat::getCnName).reversed());
        System.out.println("------- list.sort() -------");
        catList.stream().forEachOrdered(System.out::println);
        /*
            总结：
                1. 无参的sorted()是对某一项的排序，默认返回natural order，返回的是Stream<T>
                2. 带参的sorted(Comparator<? super T> comparator)可对实体类中的某项排序，默认natural order，可通过reversed()方法倒序，返回的是实体类的集合.
                3. 亦可使用java8 新的排序方式 list.sort(Comparator<? super E> c)，配合Comparator进行排序
         */

        // 7. limit(long maxSize)
        List<Cat> limitCatList = catList.stream().limit(2).collect(toList());
        System.out.println("------ limit line ------");
        limitCatList.stream().forEach(cat -> {
            System.err.println(cat.toString());
        });

        // 8. skip(long n)
        List<Cat> skipCatList = catList.stream().skip(1).collect(toList());
        System.out.println("------ skip line ------");
        skipCatList.parallelStream().forEach(cat -> {
            System.err.println(cat.toString());
        });

        // 9. min(Comparator<? super T> comparator) 最小的
        Cat minAgeOfCat = catList.parallelStream().min(Comparator.comparing(Cat::getAge)).get();
        System.out.println("------ min line ------");
        System.err.println(minAgeOfCat.toString());

        // 10. max(Comparator<? super T> comparator) 最大的
        Cat maxAgeOfCat = catList.parallelStream().max(Comparator.comparing(Cat::getAge)).get();
        System.out.println("----- max line ------");
        System.err.println(maxAgeOfCat.toString());

        // 11. count
        long count = catList.parallelStream().count();
        System.out.println("----- count line -----");
        System.err.println(count);

        // 12. anyMatch(Predicate<? super T> predicate)
        boolean isAnyMatch = catList.parallelStream().anyMatch(cat -> cat.getCnName().contains("奶"));
        System.out.println("----- anyMatch line -----");
        System.err.println(isAnyMatch);

        // 13. findAny
        Cat findAnyOfCat = catList.parallelStream().filter(cat -> cat.getCnName().contains("奶")).findAny().get();
        System.out.println("------ findAny line ------");
        System.err.println(findAnyOfCat.toString());

        /*
            总结：很多时候anyMatch与findAny可以相互替换，使用方式类似
         */

        // 14. allMatch(Predicate<? super T> predicate) 每个元素都必须匹配
        boolean isAllMatch = catList.parallelStream().allMatch(cat -> Objects.equals(cat.getCnName(), "奶酪"));
        System.out.println("------ allMatch line -------");
        System.err.println(isAllMatch);

        System.out.println();
        System.out.println("-------------------------------------  分割线  ----------------------------------------------");
        System.out.println("---------- the next methods are all for class Collectors --------------");
        System.out.println();

        // 1. Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) 返回有序列表
        List<Cat> toCollectionMethodList = catList.stream().limit(3).collect(toCollection(LinkedList::new));
        System.out.println("------- Collectors.toCollection line ---------");
        toCollectionMethodList.stream().forEach(System.err::println);

        // 2. Collector<T, ?, List<T>> toList()
        List<Cat> toListMethodList = catList.stream().skip(2).collect(toList());
        System.out.println("------- Collectors.toList line -------");
        toListMethodList.stream().forEach(System.err::println);

        // 3. Collector<T, ?, Set<T>> toSet() 过滤去重列表
        Set<Integer> toSetMethodSet = dupIntegerInitList().stream().collect(toSet());
        System.out.println("------- Collectors.toSet line --------");
        toSetMethodSet.stream().forEach(item -> {
            System.err.print(item + " ");
        });

        // 4. Collector<CharSequence, ?, String> joining()
        String cnNameJoiningStr = catList.stream().map(Cat::getCnName).collect(joining());
        System.out.println();
        System.out.println("------- Collectors.joining line -------");
        System.err.println(cnNameJoiningStr);

        // 5. Collector<CharSequence, ?, String> joining(CharSequence delimiter)
        String cnNameJoiningWithDelimiterStr = catList.stream().map(Cat::getCnName).collect(Collectors.joining(" && "));
        System.out.println();
        System.out.println("------ Collectors.joining(CharSequence delimiter) line --------");
        System.err.println(cnNameJoiningWithDelimiterStr);

        // 6. Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
        String cnNameJoiningWithPrefixAndSuffixStr = catList.stream().map(Cat::getCnName).collect(joining(" && ", "pre ", " suf"));
        System.out.println();
        System.out.println("------ Collectors.joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) line --------");
        System.err.println(cnNameJoiningWithPrefixAndSuffixStr);

        /*
            7. Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, Collector<? super U, Sort, R> downstream)
            mapping中有一段注释非常有用，需要仔细揣摩：
                The {@code mapping()} collectors are most useful when used in a multi-level reduction, such as downstream of a {@code groupingBy} or
                {@code partitioningBy}. for example:
                Map<City, Set<String>> lastNamesByCity = people.stream().collect(groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));
         */
        Map<Integer, Set<String>> mappingMethod = catList.stream().collect(groupingBy(Cat::getAge, mapping(Cat::getCnName, toSet())));
        System.out.println();
        System.out.println("-------- Collectors.mapping line --------");
        mappingMethod.values().stream().forEach(cnNameStr -> {
            System.err.print(cnNameStr + " && ");
        });

        // 8. Collector<T,Sort,RR> collectingAndThen(Collector<T,Sort,R> downstream, Function<R,RR> finisher)
        String collectingAndThenStr = catList.parallelStream().map(x -> x.getCnName()).collect(collectingAndThen(Collectors.joining(" && ", "pre ", " suf "), k -> k + " end "));
        System.out.println();
        System.out.println("------ Collectors.collectingAndThen line -------");
        System.err.println(collectingAndThenStr);

        // 9. Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)
        Optional<Cat> minByAgeOfCat = catList.parallelStream().collect(minBy(Comparator.comparing(Cat::getAge)));
        System.out.println();
        System.out.println("-------- Collectors.minBy line ---------");
        System.out.println(minByAgeOfCat.get().toString());

        // 10. Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)
        Optional<Cat> maxByAgeOfCat = catList.parallelStream().collect(maxBy(Comparator.comparing(Cat::getAge)));
        System.out.println();
        System.out.println("-------- Collectors.maxBy line ---------");
        System.out.println(maxByAgeOfCat.get().toString());

        // 11. Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)
        int totalAgeOfCat = catList.parallelStream().collect(summingInt(Cat::getAge));
        System.out.println();
        System.out.println("------- Collectors.summingInt line -------");
        System.out.println(totalAgeOfCat);

        // 12. Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)
        double averagingOfCat = catList.parallelStream().collect(averagingInt(Cat::getAge));
        System.out.println();
        System.out.println("------ Collector.averagingInt line -------");
        System.out.println(averagingOfCat);

        // 13. Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
        Map<Integer, List<Cat>> groupingByAgeOfCatMapList = catList.parallelStream().collect(groupingBy(Cat::getAge));
        System.out.println();
        System.out.println("------ Collector.groupingBy(one param) line -------");
        System.err.println("the type of map is HashMap : " + (groupingByAgeOfCatMapList instanceof HashMap));
        groupingByAgeOfCatMapList.forEach((k, v) -> {
            System.err.println("type of v(list) is ArrayList : " + (v instanceof ArrayList));
            v.parallelStream().forEach(System.err::println);
        });

        // 14. Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, Sort, D> downstream)
        Map<Integer, List<Cat>> groupByAgeOfCatMapList1 = catList.parallelStream().collect(groupingBy(Cat::getAge, toCollection(LinkedList::new)));
        System.out.println();
        System.out.println("------ Collectors.groupingBy(two param) line -------");
        System.err.println("the type of map is HashMap : " + (groupByAgeOfCatMapList1 instanceof HashMap));
        groupByAgeOfCatMapList1.forEach((k, v) -> {
            System.err.println("type of v(list) is LinkedList : " + (v instanceof LinkedList));
            v.parallelStream().forEach(System.err::println);
        });

        // 15.  Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, Sort, D> downstream)
        Map<Integer, List<Cat>> groupByAgeOfCatMapList2 = catList.parallelStream().collect(groupingBy(Cat::getAge, LinkedHashMap::new, toCollection(ArrayList::new)));
        System.out.println();
        System.out.println("----- Collectors.groupingBy(three param) line ------");
        System.err.println("the type of map is HashMap : " + (groupByAgeOfCatMapList2 instanceof HashMap));
        groupByAgeOfCatMapList2.forEach((k, v) -> {
            System.err.println("type of v(list) is ArrayList : " + (v instanceof ArrayList));
            v.parallelStream().forEach(System.err::println);
        });

        // 16. Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent(Function<? super T, ? extends K> classifier)
        Map<Integer, List<Cat>> groupingByAgeOfCatConcurrentMapList = catList.parallelStream().collect(groupingByConcurrent(Cat::getAge));
        System.out.println();
        System.out.println("----- Collectors.groupingByConcurrent(one param) line ------");
        System.err.println("the type of map is ConcurrentHashMap : " + (groupingByAgeOfCatConcurrentMapList instanceof ConcurrentHashMap));
        groupingByAgeOfCatConcurrentMapList.forEach((k, v) -> {
            System.err.println("type of v(list) is ArrayList : " + (v instanceof ArrayList));
        });

        // 17. Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, Sort, D> downstream)
        Map<Integer, List<Cat>> groupingByAgeOfCatConcurrentMapList1 = catList.parallelStream().collect(groupingByConcurrent(Cat::getAge, toCollection(LinkedList::new)));
        System.out.println();
        System.out.println("------ Collectors.groupingByConcurrent(two param) line ------");
        System.err.println("the type of map is ConcurrentHashMap : " + (groupingByAgeOfCatConcurrentMapList1 instanceof ConcurrentHashMap));
        groupingByAgeOfCatConcurrentMapList1.forEach((k, v) -> {
            System.err.println("type of v(list) is LinkedList : " + (v instanceof LinkedList));
        });

        // 18. Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory,  Collector<? super T, Sort, D> downstream)
        Map<Integer, List<Cat>> groupingByAgeOfCatConcurrentMapList2 = catList.parallelStream().collect(groupingByConcurrent(Cat::getAge, ConcurrentSkipListMap::new, toCollection(LinkedList::new)));
        System.out.println();
        System.out.println("------ Collectors.groupingByConcurrent(three param) line ------");
        System.err.println("the type of map is ConcurrentSkipListMap : " + (groupingByAgeOfCatConcurrentMapList2 instanceof ConcurrentSkipListMap));
        groupingByAgeOfCatConcurrentMapList2.forEach((k, v) -> {
            System.err.println("type of v(list) is LinkedList : " + (v instanceof LinkedList));
        });

        // 19. Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
        Map<Boolean, List<Cat>> partitioningByAgeOfCatList = catList.parallelStream().collect(partitioningBy(x -> x.getAge() > 1));
        System.out.println();
        System.out.println("------ Collectors.partitioningBy(one param) line ------");
        System.err.println("the type of map is HashMap : " + (partitioningByAgeOfCatList instanceof HashMap));
        partitioningByAgeOfCatList.forEach((k, v) -> {
            System.err.println("type of v(list) is ArrayList : " + (v instanceof ArrayList));
        });

        // 20. Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, Sort, D> downstream)
        Map<Boolean, List<Cat>> partitioningByAgeOfCatList1 = catList.parallelStream().collect(partitioningBy(x -> x.getAge() < 3, toCollection(LinkedList::new)));
        System.out.println();
        System.out.println("------ Collectors.partitioningBy(two param) line ------");
        System.err.println("the type of map is HashMap : " + (partitioningByAgeOfCatList1 instanceof HashMap));
        System.err.println("the type of map is LinkedHashMap : " + (partitioningByAgeOfCatList1 instanceof LinkedHashMap));
        System.err.println("the type of map is TreeMap : " + (partitioningByAgeOfCatList1 instanceof TreeMap));
        System.err.println("the type of map is Hashtable : " + (partitioningByAgeOfCatList1 instanceof Hashtable));
        System.err.println("the type of map is ConcurrentHashMap : " + (partitioningByAgeOfCatList1 instanceof ConcurrentHashMap));
        System.err.println("the type of map is ConcurrentSkipListMap : " + (partitioningByAgeOfCatList1 instanceof ConcurrentSkipListMap));
        System.err.println("the type of map is Map : " + (partitioningByAgeOfCatList1 instanceof Map));
        partitioningByAgeOfCatList1.forEach((k, v) -> {
            System.err.println("type of v(list) is LinkedList : " + (v instanceof LinkedList));
        });

        /*
            总结：partitionBy返回的Map直接是一个Map接口，没有具体的实现类。且，返回的Map结构中，以true与false为key的值都有，在处理值的时候根据需求过滤。
            // 21. 下面尝试下变种的一些写法: 返回值的核心都是list
         */
        Map<Boolean, List<String>> partitioningByAgeOfCatList2 = catList.parallelStream().collect(partitioningBy(k -> k.getAge() < 3, mapping(Cat::getCnName, toCollection(LinkedList::new))));
        System.out.println();
        System.out.println("----- Collectors.partitioningBy(two param) [other way to result] line -------");
        System.err.println("the type of map is Map : " + (partitioningByAgeOfCatList2 instanceof Map));
        // Map<false, List<String>> && Map<true, List<String>>
        partitioningByAgeOfCatList2.forEach((k, v) -> {
            if (k) {
                v.parallelStream().forEach(System.err::println);
            }
        });

        // 22. Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
        Map<Integer, String> toMapOfCatList = Arrays.asList(new Cat(1, "新来的", "new1", true)
                , new Cat(2, "是我呀", "new2", false))
                .parallelStream().collect(toMap(Cat::getAge, Cat::getCnName));
        System.out.println();
        System.out.println("------ Collectors.toMap(two param) line -------");
        System.err.println("the type of map is HashMap : " + (toMapOfCatList instanceof HashMap));
        toMapOfCatList.forEach((k, v) -> {
            System.err.println(v);
        });

        // 23. Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
        Map<Integer, String> toMapOfCatList1 = catList.parallelStream().collect(toMap(Cat::getAge, Cat::getEnName, (e1, e2) -> e1 + e2));
        System.out.println();
        System.out.println("------ Collectors.toMap(three param) line -------");
        System.err.println("the type of map is HashMap : " + (toMapOfCatList1 instanceof HashMap));
        toMapOfCatList1.forEach((k, v) -> {
            System.err.println(v);
        });

        /*
            24. Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper,
                                         BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
         */
        Map<Integer, String> toMapOfCatList2 = catList.parallelStream().collect(toMap(Cat::getAge, Cat::getEnName, (e1, e2) -> e1, ConcurrentHashMap::new));
        System.out.println();
        System.out.println("------ Collectors.toMap(four param) line -------");
        System.err.println("the type of map is ConcurrentHashMap : " + (toMapOfCatList2 instanceof ConcurrentHashMap));
        toMapOfCatList2.forEach((k, v) -> {
            System.err.println(v);
        });

        // one. list to map -->
        Map<Integer, String> toMapOfCatList3 = catList.parallelStream()
                .sorted(Comparator
                        .comparing(Cat::getAge)
                        .thenComparing(Cat::getEnName)
                        .reversed())
                .collect(toMap(Cat::getAge, Cat::getEnName, (e1, e2) -> e1, ConcurrentSkipListMap::new));
        System.out.println();
        System.out.println("----- Collectors.toMap(four param) line [list to map, enName as value] ------");
        System.err.println("the type of map is ConcurrentSkipListMap : " + (toMapOfCatList3 instanceof ConcurrentSkipListMap));
        toMapOfCatList3.forEach((k, v) -> {
            System.err.println(v);
        });

        // two. list to map --> Cat as value
        Map<Integer, Cat> toMapOfCatList4 = catList.parallelStream().collect(toMap(Cat::getAge, Function.identity(), (e1, e2) -> e1, ConcurrentHashMap::new));
        System.out.println();
        System.out.println("------ Collectors.toMap(four param) line [list to map, Cat as value]");
        System.err.println("the type of map is ConcurrentHashMap : " + (toMapOfCatList4 instanceof ConcurrentHashMap));
        toMapOfCatList4.forEach((k, v) -> {
            System.err.println(v);
        });

        // map to another map, firstDemo
        Map<Integer, Integer> resultMap = new LinkedHashMap<>();
        initMap().entrySet().parallelStream().sorted(Map.Entry.comparingByKey()).forEachOrdered(e -> resultMap.put(e.getKey(), e.getValue()));
        // initMap().entrySet().parallelStream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).forEachOrdered(e -> resultMap.put(e.getKey(), e.getValue()));
        System.out.println();
        System.out.println("----- map to map line [order by key] -------");
        resultMap.entrySet().forEach(entry -> {
            System.err.println(" ordered by key , key is : " + entry.getKey() + " , value is : " + entry.getValue());
        });

        // map to another map, secondDemo
        /*
            Map<Integer, Integer> resultMap1 = initMap().entrySet().parallelStream().sorted(Map.Entry.comparingByKey()).collect(toMap())
            这个方法行不通，toMap中不知道怎么写
         */
        Map<Integer, Integer> resultMap1 = new LinkedHashMap<>();
        initMap().entrySet().parallelStream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).forEachOrdered(e -> resultMap1.put(e.getKey(), e.getValue()));
        System.out.println();
        System.out.println("------ map to map line [order by value] -----");
        resultMap1.entrySet().stream().forEach(entry -> {
            System.err.println(" ordered by value, key is : " + entry.getKey() + " , value is : " + entry.getValue());
        });


    }

    private static Map<Integer, Integer> initMap() {
        return new HashMap<Integer, Integer>() {{
            put(1, 10);
            put(3, 12);
            put(2, 11);
            put(4, 13);
        }};
    }

    private static void readMeFirst() {
        /*
            1.Stream 流的常规操作可归类如下：
            . intermediate(中间流 [可一个或多个])
                map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
            . terminal(终结流 [只可有一个])
                forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
            . short-circuiting(短路 [可随时终止])
                anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 limit

         */

    }

    private static List<Integer> dupIntegerInitList() {
        return new ArrayList<Integer>() {{
            add(1);
            add(1);
            add(2);
            add(3);
        }};
    }

    private static List<Cat> dupCatInitList() {
        return new ArrayList<Cat>() {{
            add(new Cat(1, "刘啵啵儿", "liuboboer", true));
            add(new Cat(1, "刘啵啵儿", "liuboboer", true));
        }};
    }

    private static List<Cat> initList() {
        return new ArrayList<Cat>() {{
            add(new Cat(2, "奶油", "cream", true));
            add(new Cat(2, "奶酪", "cheese", true));
            add(new Cat(3, "曾三妹", "sisterThree", false));
            add(new Cat(4, "七夕", "seventh", true));
        }};
    }

    private static List<List<Integer>> flatMapInitList() {
        return new ArrayList<List<Integer>>() {{
            add(Arrays.asList(1, 2, 3));
            add(Arrays.asList(4, 5));
            add(Arrays.asList(6, 7, 8));
        }};
    }

}

class Cat {

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 中文名称
     */
    private String cnName;

    /**
     * 英文名称
     */
    private String enName;

    /**
     * 是否为男性
     */
    private Boolean isMale;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCnName() {
        return cnName;
    }

    public void setCnName(String cnName) {
        this.cnName = cnName;
    }

    public String getEnName() {
        return enName;
    }

    public void setEnName(String enName) {
        this.enName = enName;
    }

    public Boolean getMale() {
        return isMale;
    }

    public void setMale(Boolean male) {
        isMale = male;
    }

    public Cat() {
    }

    public Cat(Integer age, String cnName, String enName, Boolean isMale) {
        this.age = age;
        this.cnName = cnName;
        this.enName = enName;
        this.isMale = isMale;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "age=" + age +
                ", cnName='" + cnName + '\'' +
                ", enName='" + enName + '\'' +
                ", isMale=" + isMale +
                '}';
    }
}